﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JXT.PrimaryKey.Batman.Collections;
using JXT.PrimaryKey.Batman.Domain.Models;
using JXT.PrimaryKey.Batman.Domain.Services;
using JXT.PrimaryKey.Batman.Domain.Specifications;
using JXT.PrimaryKey.Batman.Domain.UserSystem.Models;

namespace JXT.PrimaryKey.Batman.Domain.UserSystem.Services
{
    public interface IUserService : ICachedService
    {
        User Register(string name, string mail, string password, string realName, Gender gender, string identity, string registerIp, IEnumerable<Role> roles);

        string GetDefaultOAuthPassword(User entity);

        void UpdateStatus(User entity, string loginIp, string token = null);

        /// <summary>
        /// 获取已登录用户资料，若token过期，自动删除登录记录并返回null
        /// </summary>
        /// <param name="token">登录令牌</param>
        /// <returns>登录过的完整用户信息</returns>
        User GetLoginSingle(string token);

        void Save(User entity);

        void Delete(User entity);

        bool Exists(ISpecification<User> specification);

        int Count(ISpecification<User> specification);

        User GetSingle(int userId);

        User GetSingle(ISpecification<User> specification);

        IPagedCollection<User> GetList(IPagedSpecification<User> specification);

        UserDetail GetDetail(User user);

        UserStatus GetStatus(User user);

        IEnumerable<Role> GetRoles(User user);

        void UpdateDetail(User user, UserDetail detail);

        void SetAuthority(User user, Authority[] authorityArray);

        void SetRoles(User user, IEnumerable<Role> roles);

        bool ExistsName(string username);

        bool ExistsMail(string mail);

        /// <summary>
        /// 用户登录验证方法
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="mail">邮箱</param>
        /// <param name="password">用户密码</param>
        /// <param name="loginIp">登录IP</param>
        /// <param name="expires">过期时间</param>
        /// <returns>用户的登录状态</returns>
        LoginStatus Login(string name, string mail, string password, string loginIp, DateTime? expires = null);

        /// <summary>
        /// 第三方登录验证方法
        /// </summary>
        /// <returns></returns>
        LoginStatus Login(OAuthUserBinding binding, string oauthToken, string oauthTokenSecret, string oauthUserId, string oauthVerifier, string loginIp, DateTime? expires = null);

        /// <summary>
        /// 用户登出
        /// </summary>
        /// <param name="token">用户的登录令牌</param>
        /// <param name="sysId">系统用户id</param>
        void Logout(string token, string sysId = null);

        /// <summary>
        /// 添加微博用户和本地用户的关联
        /// </summary>
        /// <param name="userId">本地用户Id</param>
        /// <param name="blogUserId">微博用户id(或者name)</param>
        /// <param name="mark">标示是哪一方的微博用户</param>
        void AddOAuthUserBinding(OAuthUserBinding userBinding);

        OAuthUserBinding GetOAuthUserBindingSingle(ISpecification<OAuthUserBinding> specification);

        void DeleteOAuthUserBinding(OAuthUserBinding binding);

        IEnumerable<OAuthUserBinding> GetOAuthUserBindingList(ISpecification<OAuthUserBinding> specification);

        /// <summary>
        /// 根据第三方标示和用户获取该用户在本系统里的用户名
        /// </summary>
        /// <param name="blogUserId">第三方用户Id</param>
        /// <param name="oAuthId">标示属于哪一方(sina,tencent or other)</param>
        /// <returns>MicroBlogUser对象</returns>
        OAuthUserBinding GetOAuthUserBindingSingle(string userIdentity, int oAuthId);

        /// <summary>
        /// 事务方式添加bloguser和user
        /// </summary>
        /// <param name="username">昵称姓名</param>
        /// <param name="blogUserId">第三方用户id</param>
        /// <param name="mark">标示属于哪一方</param>
        void RegisterAndBindingToOAuth(string username, string mail, string password, OAuthUserBinding binding);

        /// <summary>
        /// 已有用户绑定的
        /// </summary>
        /// <param name="username"></param>
        /// <param name="mail"></param>
        /// <param name="password"></param>
        /// <param name="blogUserId"></param>
        /// <param name="mark"></param>
        void BindingToOAuth(string username, string mail, string password, OAuthUserBinding binding);
    }
}
